#include <gtk/gtk.h>
#include "chatRoom.h"
gchar *titles[6] = {"房间名", "在线人数", "房主", "房间描述","是否要密码","ID",};
enum {
	ROOMNAME,
    MEMNUM,
    HOSTNAME,
    DISCRIPTION,
    ISNEEDPASSWD,
    ID,
    N_COLUMNS
};
const gchar *new_row[6];
//static gchar *message;


GtkWidget *window;


pchatRoom pcrMain;
static GtkWidget *clist;
GtkListStore *list_store;
static GtkWidget *add_win;
static GtkWidget *chat_publicroom;
static GtkWidget *entry_name;
static GtkWidget *entry_tip;
static GtkWidget *entry_password;
static GtkWidget *entry_creator;
GtkWidget* textup;
GtkTextBuffer *bufferup,*bufferdown;
gint current_row = 0;	//当前行
gint row_count = 0;	//总行数
int roomID = 0;
int whoID = 0;
int serverExit = 0;
int clientExit = 0;
int port = 8182;
int hostID = -1;
pclientinfoFS clientlinkFS;
pclientinfoFC clientlinkFC;
int sockfd;



void * multiThreadForC(void* p);
void * multiThread(void* p);
void list_selection_changed_cb (GtkTreeSelection *selection, gpointer data);
void add_columns (GtkTreeView *treeview);
void on_off_clicked(GtkButton *button, gpointer data);
void* handleaccept(void *serverfd);
void handlerecvmsg(int *sockfd);
void send_messageFS(GtkButton *button, gpointer data);
void send_messageFC(GtkButton *button, gpointer data);

/*void closeWindowForServer(GtkWidget* window,gpoint data){
	gtk_widget_destroy(chat_publicroom);
	gtk_widget_show_all(window);
}
void closeWindowForClient(GtkWidget* window,gpoint data){
	gtk_widget_destroy(chat_publicroom);
	gtk_widget_show_all(window);
}*/

GtkWidget* show_publicroomForHost(void)
{
	struct sockaddr_in server_addr;
	pthread_t pid;
	serverExit = 0;
	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
		    {
					printf("创建socket失败\n");
					exit(1);
		    }
		    clientlinkFS = CreateLinkList();
		    clientlinkFS->sockfd = sockfd;

		    bzero(&server_addr,sizeof(struct sockaddr_in));
		    server_addr.sin_family = AF_INET;
		    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
		    server_addr.sin_port = htons(port);
			if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
		    {
				printf("绑定端口失败,端口可能被占用\n");
				exit(2);
			}
		    if(listen(sockfd,20) == -1)
		    {
					printf("监听端口失败\n");
					exit(3);
		    }

		    printf("正在port:%d监听中...\n",port);
		    pthread_create(&pid,NULL,(void *)handleaccept,(void *)&sockfd);
		    //pthread_create(&pid,NULL,(void *)multiThread,(void *)pcr);

		    //创建界面
	GtkWidget* chat_window;
	GtkWidget* scrolledwin;
	GtkWidget* text;
	GtkWidget *hPaned, *vPaned;
	GtkWidget* button;
	GtkWidget* vbox, *bbox;

	chat_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	//g_signal_connect(GTK_OBJECT(chat_window), "delete_event", GTK_SIGNAL_FUNC(closeWindowForServer), chat_window);
	gtk_window_set_title(GTK_WINDOW(chat_window), pcrMain->hostName);
	gtk_window_set_position(GTK_WINDOW(chat_window), GTK_WIN_POS_CENTER);
	gtk_container_set_border_width(GTK_CONTAINER(chat_window),10);
	gtk_window_set_default_size(GTK_WINDOW(chat_window), 500, 400);

	hPaned = gtk_hpaned_new();
	vPaned = gtk_vpaned_new();
	gtk_container_add(GTK_CONTAINER(chat_window), hPaned);
	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_size_request(scrolledwin, 320, 250);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	textup = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textup), GTK_WRAP_WORD_CHAR);
	gtk_container_add(GTK_CONTAINER(scrolledwin), textup);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(textup), FALSE);
	bufferup = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textup));


	gtk_paned_add1(GTK_PANED(hPaned), vPaned);
	gtk_paned_add2(GTK_PANED(hPaned), gtk_frame_new("房间在线用户"));

	gtk_paned_add1(GTK_PANED(vPaned), scrolledwin);
	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	text = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
	gtk_container_add(GTK_CONTAINER(scrolledwin), text);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(text), TRUE);
	bufferdown = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_paned_add2(GTK_PANED(vPaned), vbox);
	gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);

	bbox = gtk_hbutton_box_new();
	gtk_box_set_spacing(GTK_BOX(bbox), 10);
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
	gtk_button_box_set_child_size(GTK_BUTTON_BOX(bbox), 20, 20);
	gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
	button = gtk_button_new_with_label("发送");
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(send_messageFS), NULL);
	button = gtk_button_new_with_label("关闭");
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
	//g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(closeWindowForServer), NULL);
	button = gtk_button_new_with_label("T出房间");
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
	//g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(on_off_clicked), NULL);


	gtk_widget_show_all(chat_window);

	return chat_window;
}

void on_ok_clicked (GtkButton *button, gpointer data)
{
	pthread_t pid;
	new_row[0] = gtk_entry_get_text(GTK_ENTRY(entry_name));
	//new_row[1] = gtk_entry_get_text(GTK_ENTRY(entry_tip));
	new_row[1] = "1";
	new_row[2] = gtk_entry_get_text(GTK_ENTRY(entry_creator));
	new_row[3] = gtk_entry_get_text(GTK_ENTRY(entry_tip));

	pchatRoom pcr=(pchatRoom)malloc(sizeof(chatRoom));
	bcopy(new_row[0],pcr->roomName,strlen(new_row[0])+1);
	bcopy(new_row[2],pcr->hostName,strlen(new_row[2])+1);
	bcopy(new_row[3],pcr->description,strlen(new_row[3])+1);
	//bcopy(pcr,pcrMain,sizeof(chatRoom));
	bcopy(new_row[0],pcrMain->roomName,strlen(new_row[0])+1);
	bcopy(new_row[2],pcrMain->hostName,strlen(new_row[2])+1);
	bcopy(new_row[3],pcrMain->description,strlen(new_row[3])+1);
	pthread_create(&pid,NULL,(void *)multiThread,(void *)pcr);

	//row_count++;
	//gtk_clist_append(GTK_CLIST(clist), new_row);
	gtk_widget_destroy(add_win);
	chat_publicroom = show_publicroomForHost();
	gtk_widget_show(chat_publicroom);
	gtk_widget_hide_all(window);
}

void on_cancel_clicked(GtkButton *button, gpointer data)
{
	gtk_widget_destroy(add_win);
}

void on_off_clicked(GtkButton *button, gpointer data)
{
	gtk_widget_destroy(chat_publicroom);
}

GtkWidget* create_addwin(void)
{
	GtkWidget* win;
	GtkWidget* vbox;
	GtkWidget* table;
	GtkWidget* bbox;
	GtkWidget* label;
	GtkWidget* button;

	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(win), "创建新房间");
	gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER);
	g_signal_connect(GTK_OBJECT(win), "delete_event", GTK_SIGNAL_FUNC(gtk_widget_destroy), win);
	gtk_container_set_border_width(GTK_CONTAINER(win),10);

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(win), vbox);

	table = gtk_table_new(4, 2, FALSE);
	gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 5);

	label = gtk_label_new("房间名");
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
	entry_name = gtk_entry_new();
	gtk_table_attach_defaults(GTK_TABLE(table), entry_name, 1, 2, 0, 1);
	label = gtk_label_new("房间描述");
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
	entry_tip = gtk_entry_new();
	gtk_table_attach_defaults(GTK_TABLE(table), entry_tip, 1, 2, 1, 2);
	label = gtk_label_new("密码");
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
	entry_password = gtk_entry_new();
	gtk_table_attach_defaults(GTK_TABLE(table), entry_password, 1, 2, 2, 3);
	label = gtk_label_new("姓名");
	gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
	entry_creator = gtk_entry_new();
	gtk_table_attach_defaults(GTK_TABLE(table), entry_creator, 1, 2, 3, 4);

	bbox = gtk_hbutton_box_new();
	gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 5);
	gtk_box_set_spacing(GTK_BOX(bbox), 5);
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);

	button = gtk_button_new_from_stock(GTK_STOCK_OK);
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(on_ok_clicked), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 5);
	button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(on_cancel_clicked), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 5);

	gtk_widget_show_all(win);

	return win;
}

GtkWidget* show_publicroom(void)
{
	clientExit = 0;
	struct sockaddr_in server_addr;
	pthread_t pid;
	pchatRoom pcrtmp = getByID(pcrMain,hostID);
	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
		        {
		                printf("创建socket失败\n");
		                exit(1);
		        }

		bzero(&server_addr,sizeof(struct sockaddr_in));
		server_addr.sin_family = AF_INET;
		server_addr.sin_addr = pcrtmp->addr_in.sin_addr;
		server_addr.sin_port = htons(port);
		printf("尝试连接%s,port %d\n",inet_ntoa(pcrtmp->addr_in.sin_addr),port);
		while(connect(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1)
		{
			printf("服务器无响应,2秒后再连接\n");
			sleep(2);
		}
		pthread_create(&pid,NULL,(void *)handlerecvmsg,(void *)&sockfd);
	    struct message filedata;
	    strcpy(filedata.flag,"login");
	            struct hostent* h = gethostent();
	            strcpy((char*)filedata.name,(char*)h->h_name);
	            endhostent();
	            if(send(sockfd,&filedata,sizeof(struct message),0)>0){
	              printf("send name successfully!!!\n");
	    }
	            clientlinkFC=pcCreateLinkList();


	//界面
	GtkWidget* chat_window;
	GtkWidget* scrolledwin;
	GtkWidget* text;
	GtkWidget *hPaned, *vPaned;
	GtkWidget* button;
	GtkWidget* vbox, *bbox;

	chat_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	//g_signal_connect(GTK_OBJECT(chat_window), "delete_event", GTK_SIGNAL_FUNC(closeWindowForClient), chat_window);
	gtk_window_set_title(GTK_WINDOW(chat_window), "公共房间");
	gtk_window_set_position(GTK_WINDOW(chat_window), GTK_WIN_POS_CENTER);
	gtk_container_set_border_width(GTK_CONTAINER(chat_window),10);
	gtk_window_set_default_size(GTK_WINDOW(chat_window), 500, 400);

	hPaned = gtk_hpaned_new();
	vPaned = gtk_vpaned_new();
	gtk_container_add(GTK_CONTAINER(chat_window), hPaned);
	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_widget_set_size_request(scrolledwin, 320, 250);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	textup = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textup), GTK_WRAP_WORD_CHAR);
	gtk_container_add(GTK_CONTAINER(scrolledwin), textup);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(textup), FALSE);
	bufferup = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textup));

	gtk_paned_add1(GTK_PANED(hPaned), vPaned);
	gtk_paned_add2(GTK_PANED(hPaned), gtk_frame_new("房间在线用户"));

	gtk_paned_add1(GTK_PANED(vPaned), scrolledwin);
	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	text = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
	gtk_container_add(GTK_CONTAINER(scrolledwin), text);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(text), TRUE);
	bufferdown = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_paned_add2(GTK_PANED(vPaned), vbox);
	gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);

	bbox = gtk_hbutton_box_new();
	gtk_box_set_spacing(GTK_BOX(bbox), 10);
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
	gtk_button_box_set_child_size(GTK_BUTTON_BOX(bbox), 20, 20);
	gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
	button = gtk_button_new_with_label("发送");
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(send_messageFC), NULL);
	button = gtk_button_new_with_label("关闭");
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
	//g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(closeWindowForClient), NULL);


	gtk_widget_show_all(chat_window);

	return chat_window;
}

void closeApp(GtkWidget *window, gpointer data)
{
	gtk_main_quit();
}

GtkWidget* create_button (gchar *stockid)
{
	GtkWidget *button;
	GtkWidget *image;
	image = gtk_image_new_from_stock(stockid, GTK_ICON_SIZE_MENU);
	button = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button), image);
	return button;
}

void goto_first(GtkButton *button, gpointer data)
{
	current_row = 0;
	gtk_clist_select_row(GTK_CLIST(clist), current_row, 0);
}

void goto_last(GtkButton *button, gpointer data)
{
	current_row = row_count - 1;
	gtk_clist_select_row(GTK_CLIST(clist), current_row, 0);
}

void go_back(GtkButton *button, gpointer data)
{
	current_row--;
	if(current_row == -1) return ;
	gtk_clist_select_row(GTK_CLIST(clist), current_row, 0);
}

void go_forward(GtkButton *button, gpointer data)
{
	current_row++;
	if(current_row > row_count) return ;
	gtk_clist_select_row(GTK_CLIST(clist), current_row, 0);
}

void go_room(GtkButton *button, gpointer data)
{
	if(hostID<0){
		GtkWidget* dialog = gtk_message_dialog_new(window,
				                           GTK_DIALOG_DESTROY_WITH_PARENT,
				                           GTK_MESSAGE_WARNING,
				                           GTK_BUTTONS_OK,
				                           "请选择一个房间再加入！！！");
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
	}
	chat_publicroom = show_publicroom();
	gtk_widget_show(chat_publicroom);
}

void append_row(GtkButton *button, gpointer data)
{
	add_win = create_addwin();
	gtk_widget_show(add_win);
}

int main(int argc, char *argv[])
{
	pcrMain=crCreateLinkList();
	pthread_t pid;
	pthread_create(&pid,NULL,(void *)multiThreadForC,(void *)0);
	GtkWidget *vbox;
	GtkWidget *button;
	GtkWidget *bbox;
	GtkTooltips *button_tips;

	gtk_init(&argc, &argv);
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(closeApp), NULL);
	gtk_window_set_title(GTK_WINDOW(window), "聊天大厅");
	gtk_container_set_border_width(GTK_CONTAINER(window), 20);
	gtk_window_set_default_size(GTK_WINDOW(window), 600, 500);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(window), vbox);


	list_store = gtk_list_store_new (N_COLUMNS,
			                           G_TYPE_STRING,//ROOMNAME
			                           G_TYPE_INT,//MEMNUM
	                                   G_TYPE_STRING,//HOSTNAME
	                                   G_TYPE_STRING,//DES
	                                   G_TYPE_STRING,//PASSWD
	                                   G_TYPE_INT//ID
	                                   );
	clist=gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
	add_columns((GtkTreeView*)clist);

	GtkTreeSelection *select;

	select = gtk_tree_view_get_selection (GTK_TREE_VIEW (clist));
	gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
	g_signal_connect (G_OBJECT (select), "changed",
	                  G_CALLBACK (list_selection_changed_cb),
	                  NULL);

	//clist = gtk_clist_new_with_titles(6, titles);
	gtk_box_pack_start(GTK_BOX(vbox), clist, TRUE, TRUE, 5);

	bbox = gtk_hbutton_box_new();
	gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 5);
	gtk_box_set_spacing(GTK_BOX(bbox), 5);
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
	gtk_button_box_set_child_size(GTK_BUTTON_BOX(bbox), 20, 20);
	button_tips = gtk_tooltips_new();

	button = create_button(GTK_STOCK_YES);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "加入该房间", "加入");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(go_room), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	button = create_button(GTK_STOCK_GOTO_FIRST);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "转到首间","首间");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(goto_first), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	button = create_button(GTK_STOCK_GO_BACK);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "转到前一房间","前一房间");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(go_back), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	button = create_button(GTK_STOCK_GO_FORWARD);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "转到下一房间","下一房间");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(go_forward), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	button = create_button(GTK_STOCK_GOTO_LAST);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "转到尾间","尾间");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(goto_last), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	button = create_button(GTK_STOCK_ADD);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "新增房间","新增");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(append_row), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	button = create_button(GTK_STOCK_QUIT);
	gtk_tooltips_set_tip(GTK_TOOLTIPS(button_tips), button, "退出","退出");
	g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 2);

	gtk_widget_show_all(window);
	gtk_main();

	return FALSE;
}
